/**
 * @class   wosh::devices::WindowsMobileBundle
 * @brief   \ref wosh::interfaces::services::CommunicatorService "Communicator Service"
 *          designed to run on Windows CE/Mobile 6.x, wrapper to TAPI layers: providides
 *          (SMS) messaging, line monitoring.
 *
 * This bundle is meant to run ON a Windows Mobile 6 device.
 *  http://nah6.com/~itsme/cvs-xdadevtools/itsutils/leds/
 *
 *
 * Requirements:
 *  - You need Visual Studio 2008 (probably will wosk with 2005 too)
 *  - Windows Mobile SDK 6
 *  - QT Source for Windows (and build on your system)
 *  - Build WINCESRV target using 'wince.pri', executing 'qmake -tp vc' and building with VS
 *
 *
 * Installation on UNIX
 *  Steps:
 *  - Setup RNDIS driver
 *  - Configure Network
 *  - Install and start wincesv on device
 *
 *
 * @section Setup RNDIS driver
 * Verify USB link: plug device and type 'lsusb' to ensure device has been found.
 * Goto http://www.synce.org and follow instructions (reported here too)
 *
 * Bundle has beed tested using usb-rndis-lite-0.11.tar.gz (provided in /extras)
 * Make sure you have linux-headers installed (same release as current image).
 * Unplug device.
 *
 * @code
 *  $ tar zxf usb-rndis-lite-0.11.tar.gz
 *  $ cd usb-rndis-lite-0.11/
 *  $ make
 *  $ sudo ./clean.sh
 *  $ sudo make install
 *  $ cd ..
 * @endcode
 *
 * Make sure module has been installed and loaded executing 'lsmod | grep rndis'
 * Plug device, type \c dmesg (it should show you success/failure log)
 *
 * Type 'ifconfig -a' to list ALL interfaces, default name is \c rndis0 but it may
 * be different, check the HWaddress to recognize it and activace DHCP client:
 * 
 * @code
 *  $ dhclient3 rndis0
 * @endcode
 * 
 * Check again with \c ifconfig now it should be listed without '-a' and with a valid
 * IP (usually 169.254.2.2).
 * Confirm connection pinging device: ping 169.254.2.2
 *
 *----------------------------------------------------------------------------------
 *
 * in /etc/network/interfaces add:
 * @code
 *  iface rndis0 inet dhcp
 *  auto rndis0
 * @endcode
 *
 *
 *
 * @ingroup Devices
 ****************************************************************************
 * @version $Id: WindowsMobileBundle.h 2846 2010-08-06 12:18:31Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Devices_WindowsMobile_H__
 #define __WOSH_Bundles_Devices_WindowsMobile_H__

 #include <core/BundleGeneric.h>

 #include <framework/communication/TextMessage.h>

 #include <framework/security/SecurityAuthentication3rdParty.h>

 #include <interfaces/Device.h>
 #include <interfaces/devices/Phone.h>
 #include <interfaces/devices/WindowsMobile.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <core/Communicator.h>

 #include "IWindowsMobileListener.h"

namespace wosh {
 namespace devices {

 #define _WindowsMobile_NAME					"WindowsMobile"
 #define _WindowsMobile_VERSION					0.705


class WindowsMobileBundle :	public BundleGeneric, 
							public virtual wosh::interfaces::services::CommunicatorService,
							public virtual wosh::interfaces::devices::Phone,
							public virtual IWindowsMobileListener
 {
	WOSH_BUNDLE(wosh::devices::WindowsMobileBundle)
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::devices::Phone")
	WOSH_BUNDLE_SINGLETON(0,0)


	public:
		/**
		 * @brief   Default constructor.
		 */
		WindowsMobileBundle();
		/**
		 * @brief   Deconstructor. If bundle is running, it will be stopped.
		 */
		virtual ~WindowsMobileBundle();


	public:
		WRESULT bundleStart();
		WRESULT bundleStop();
		WRESULT bundleDiagnostic();

		void busMessage( const Message& message, const Bus* source );

//@}
/** @name Device Interface
 * @{
 ******************************************************************************/
	public:
		wosh::interfaces::Device::DEVICE_STATE getDeviceState() const { return this->devState; }
//@}
/** @name Phone Interface
 * @{
 ******************************************************************************/
	public:

//@}
/** @name Communicator Interface
 * @{
 ******************************************************************************/
	public:
		bool isUserReachable( const std::string& wosh_username );
		WRESULT sendMessageToUser( const std::string& wosh_username, const std::string& message );
		WRESULT sendMessageTo( const std::string& user_phonenumber, const std::string& message );
		WRESULT notify( const Notification* notification, const Message* notification_message = NULL );

//@}
/** @name WindowsMobile Events
 * @{
 ******************************************************************************/
 	public:
		void phone_CallState( WindowsMobileImpl* modem, VoiceCall* info );
		void phone_SmsMessage( WindowsMobileImpl* modem, const struct sms_info& info );
//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		WRESULT declareCommunicator();
		WRESULT declareUser( bool online, const std::string& username );

		WRESULT updateInfos();
		std::string getPhoneNumberFromUsername( const std::string& wosh_username );
		std::string getUsernameFromPhoneNumber( const std::string& phone_number );

		void fixPhoneNumber( std::string& phone ) const;

//@}
	private:
		Response* mmDoSendSms( const Message* requestMessage );
		Response* mmDoDialCall( const Message* requestMessage );
		Response* mmDoFakeRecSms( const Message* requestMessage );
		Response* mmDoSendMessageToUser( const Message* requestMessage );

		Response* mmDoShutdownWindows( const Message* requestMessage );

	protected:
		BusConnector BusDevices;			///< connector to Devices bus
		BusConnector BusSecurity;			///< connector to Security bus (posting only)
		WindowsMobileImpl* winWorker;		///< real implementation

		wosh::security::SecurityTokenProvider3rdparty CredentialsIO;

		wosh::interfaces::Device::DEVICE_STATE devState;	///< current state of device


}; // class def

 }; // namespace devices
}; // namespace wosh

#endif //__WOSH_Bundles_Devices_WindowsMobile_H__
